<template>
    <div class="contact-list">
        <van-nav-bar title="联系人列表" left-text="返回" left-arrow @click-left="goBack"></van-nav-bar>
        <van-contact-list v-model="chosenContactId" :list="list"  @add="onAdd" @edit="onEdit"
            @select="onSelect" />

    </div>
</template>

<script>
export default {
    props: ['id'],
    data() {
        return {
            chosenContactId: 1,
            list: [],
        };
    },
    mounted(){
        this.chosenContactId = Number(this.id)
        this.getContactAll()
    },
    methods: {
        goBack() {
            this.$router.back()
        },
        onAdd() {
            this.$router.push('/contactAdd')
        },
        onEdit(contact) {
            // Toast('编辑' + contact.id);
        },
        // 选择
        onSelect(contact) {
            this.http.put(`${this.$api.setContact}/${contact.id}`).then(res=> res.data)
                .then(res => {
                    if(res?.code == 200) {
                        this.$msg.success(res?.msg)
                        this.chosenContactId = contact.id
                    } else {
                        this.$msg.fail(res.msg)
                    }
                })
        },
        getContactAll() {
            this.http.get(this.$api.getContactAll).then(res => res.data)
                .then(res => {
                    if (res.code == 200) {
                        this.list = res.data
                        this.list.forEach(item => {
                            if(item.isDefault == 1) {
                                this.chosenContactId = item.id
                            }
                        })
                    }
                })
        }
    },
}
</script>

<style>

</style>